home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / util / moni / Scout-src.lha / source / objects / scout_lowmemory.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-09-17  |  14.5 KB  |  407 lines

  1. /**
  2.  * Scout - The Amiga System Monitor
  3.  *
  4.  *------------------------------------------------------------------
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * You must not use this source code to gain profit of any kind!
  21.  *
  22.  *------------------------------------------------------------------
  23.  *
  24.  * @author Andreas Gelhausen
  25.  * @author Richard Körber <rkoerber@gmx.de>
  26.  */
  27.  
  28. #include "system_headers.h"
  29.  
  30. struct LowMemoryCallbackUserData {
  31.     APTR ud_List;
  32.     ULONG ud_Count;
  33. };
  34.  
  35. static __asm __saveds LONG lowmemlist_con2func(register __a2 Object *obj, register __a1 struct NList_ConstructMessage *msg, register __a0 struct Hook *hook)
  36. {
  37.     return AllocListEntry(msg->pool, msg->entry, sizeof(struct LowMemoryEntry));
  38. }
  39.  
  40. MakeHook(lowmemlist_con2hook, lowmemlist_con2func);
  41.  
  42. static __asm __saveds LONG lowmemlist_des2func(register __a2 Object *obj, register __a1 struct NList_DestructMessage *msg, register __a0 struct Hook *hook)
  43. {
  44.     FreeListEntry(msg->pool, &msg->entry);
  45.  
  46.     return 0;
  47. }
  48.  
  49. MakeHook(lowmemlist_des2hook, lowmemlist_des2func);
  50.  
  51. static __asm __saveds LONG lowmemlist_dsp2func(register __a2 Object *obj, register __a1 struct NList_DisplayMessage *msg, register __a0 struct Hook *hook)
  52. {
  53.     struct LowMemoryEntry *lme = (struct LowMemoryEntry *)msg->entry;
  54.  
  55.     if (lme) {
  56.         msg->strings[0] = lme->lme_Address;
  57.         msg->strings[1] = lme->lme_Name;
  58.         msg->strings[2] = lme->lme_Type;
  59.         msg->strings[3] = lme->lme_Pri;
  60.         msg->strings[4] = lme->lme_Data;
  61.         msg->strings[5] = lme->lme_Code;
  62.     } else {
  63.         msg->strings[0] = "Address";
  64.         msg->strings[1] = "ln_Name";
  65.         msg->strings[2] = "ln_Type";
  66.         msg->strings[3] = "ln_Pri";
  67.         msg->strings[4] = "is_Data";
  68.         msg->strings[5] = "is_Code";
  69.         msg->preparses[0] = MUIX_B;
  70.         msg->preparses[1] = MUIX_B;
  71.         msg->preparses[2] = MUIX_B;
  72.         msg->preparses[3] = MUIX_B;
  73.         msg->preparses[4] = MUIX_B;
  74.         msg->preparses[5] = MUIX_B;
  75.     }
  76.  
  77.     return 0;
  78. }
  79.  
  80. MakeHook(lowmemlist_dsp2hook, lowmemlist_dsp2func);
  81.  
  82. static LONG lowmemlist_cmp2colfunc( struct LowMemoryEntry *lme1,
  83.                                     struct LowMemoryEntry *lme2,
  84.                                     ULONG column )
  85. {
  86.     LONG pri1, pri2;
  87.  
  88.     switch (column) {
  89.         case 0: return stricmp(lme1->lme_Address, lme2->lme_Address);
  90.         case 1: return stricmp(lme1->lme_Name, lme2->lme_Name);
  91.         case 2: return stricmp(lme1->lme_Type, lme2->lme_Type);
  92.         case 3: IsDec(lme1->lme_Pri, &pri1); IsDec(lme2->lme_Pri, &pri2); return pri2 - pri1;
  93.         case 4: return stricmp(lme1->lme_Data, lme2->lme_Data);
  94.         case 5: return stricmp(lme1->lme_Code, lme2->lme_Code);
  95.     }
  96. }
  97.  
  98. static __asm __saveds LONG lowmemlist_cmp2func(register __a2 Object *obj, register __a1 struct NList_CompareMessage *msg, register __a0 struct Hook *hook)
  99. {
  100.     LONG cmp;
  101.     struct LowMemoryEntry *lme1, *lme2;
  102.     ULONG col1, col2;
  103.  
  104.     lme1 = (struct LowMemoryEntry *)msg->entry1;
  105.     lme2 = (struct LowMemoryEntry *)msg->entry2;
  106.     col1 = msg->sort_type & MUIV_NList_TitleMark_ColMask;
  107.     col2 = msg->sort_type2 & MUIV_NList_TitleMark2_ColMask;
  108.  
  109.     if (msg->sort_type == MUIV_NList_SortType_None) return 0;
  110.  
  111.     if (msg->sort_type & MUIV_NList_TitleMark_TypeMask) {
  112.         cmp = lowmemlist_cmp2colfunc(lme2, lme1, col1);
  113.     } else {
  114.         cmp = lowmemlist_cmp2colfunc(lme1, lme2, col1);
  115.     }
  116.  
  117.     if (cmp != 0 || col1 == col2) return cmp;
  118.  
  119.     if (msg->sort_type2 & MUIV_NList_TitleMark2_TypeMask) {
  120.         cmp = lowmemlist_cmp2colfunc(lme2, lme1, col2);
  121.     } else {
  122.         cmp = lowmemlist_cmp2colfunc(lme1, lme2, col2);
  123.     }
  124.  
  125.     return cmp;
  126. }
  127.  
  128. MakeHook(lowmemlist_cmp2hook, lowmemlist_cmp2func);
  129.  
  130. static void ReceiveList( void (* callback)( struct LowMemoryEntry *lme, void *userData ),
  131.                          void *userData )
  132. {
  133.     struct LowMemoryEntry *lme;
  134.  
  135.     if (lme = tbAllocVecPooled(globalPool, sizeof(struct LowMemoryEntry))) {
  136.         if (SendDaemon("GetLowMemList")) {
  137.             while (ReceiveDecodedEntry((UBYTE *)lme, sizeof(struct LowMemoryEntry))) {
  138.                 callback(lme, userData);
  139.             }
  140.         }
  141.  
  142.         tbFreeVecPooled(globalPool, lme);
  143.     }
  144. }
  145.  
  146. static void IterateList( void (* callback)( struct LowMemoryEntry *lme, void *userData ),
  147.                          void *userData )
  148. {
  149.     if (CheckLibVersion(SysBase, 39, 0) == CLV_NEWER_OR_SAME) {
  150.         struct MinList tmplist;
  151.         struct LowMemoryEntry *lme, *_lme;
  152.         struct Interrupt *irq;
  153.  
  154.         NewList((struct List *)&tmplist);
  155.  
  156.         Forbid();
  157.  
  158.         ITERATE_LIST(&SysBase->ex_MemHandlers, struct Interrupt *, irq) {
  159.             if (lme = AllocVec(sizeof(struct LowMemoryEntry), MEMF_PUBLIC)) {
  160.                 lme->lme_Addr = irq;
  161.  
  162.                 _snprintf(lme->lme_Address, sizeof(lme->lme_Address), "$%08lx", irq);
  163.                 stccpy(lme->lme_Name, nonetest(irq->is_Node.ln_Name), sizeof(lme->lme_Name));
  164.                 stccpy(lme->lme_Type, GetNodeType(irq->is_Node.ln_Type), sizeof(lme->lme_Type));
  165.                 _snprintf(lme->lme_Pri, sizeof(lme->lme_Pri), "%4ld", irq->is_Node.ln_Pri);
  166.                 _snprintf(lme->lme_Data, sizeof(lme->lme_Data), "$%08lx", irq->is_Data);
  167.                 if (points2ram((APTR)irq->is_Code)) {
  168.                    _snprintf(lme->lme_Code, sizeof(lme->lme_Code), MUIX_PH "$%08lx", irq->is_Code);
  169.                 } else {
  170.                    _snprintf(lme->lme_Code, sizeof(lme->lme_Code), "$%08lx", irq->is_Code);
  171.                 }
  172.  
  173.                 AddTail((struct List *)&tmplist, (struct Node *)lme);
  174.             }
  175.         }
  176.  
  177.         Permit();
  178.  
  179.         ITERATE_CHANGING_LIST(&tmplist, struct LowMemoryEntry *, lme, _lme) {
  180.             callback(lme, userData);
  181.             FreeVec(lme);
  182.         }
  183.     }
  184. }
  185.  
  186. static void UpdateCallback( struct LowMemoryEntry *lme,
  187.                             void *userData )
  188. {
  189.     struct LowMemoryCallbackUserData *ud = (struct LowMemoryCallbackUserData *)userData;
  190.  
  191.     InsertBottomEntry(ud->ud_List, lme);
  192.     ud->ud_Count++;
  193. }
  194.  
  195. static void PrintCallback( struct LowMemoryEntry *lme,
  196.                            void *userData )
  197. {
  198.     if (points2ram((APTR)lme->lme_Addr->is_Code)) {
  199.         PrintFOneLine((BPTR)userData, " %s %-9s %4s %s %-9.9s %s\n", lme->lme_Address, lme->lme_Type, lme->lme_Pri, lme->lme_Data, lme->lme_Code + 2, lme->lme_Name);
  200.     } else {
  201.         PrintFOneLine((BPTR)userData, " %s %-9s %4s %s %-9.9s %s\n", lme->lme_Address, lme->lme_Type, lme->lme_Pri, lme->lme_Data, lme->lme_Code, lme->lme_Name);
  202.     }
  203. }
  204.  
  205. static void SendCallback( struct LowMemoryEntry *lme,
  206.                           void *userData )
  207. {
  208.     SendEncodedEntry((UBYTE *)lme, sizeof(struct LowMemoryEntry));
  209. }
  210.  
  211. static ULONG __saveds mNew( struct IClass *cl,
  212.                             Object *obj,
  213.                             struct opSet *msg )
  214. {
  215.     APTR lowmemlist, lowmemtext, lowmemcount, updateButton, printButton, removeButton, priorityButton, exitButton;
  216.  
  217.     if (obj = (Object *)DoSuperNew(cl, obj,
  218.         MUIA_HelpNode, LowMemoryText,
  219.         MUIA_Window_ID, MakeID('L','O','W','M'),
  220.         WindowContents, VGroup,
  221.  
  222.             Child, lowmemlist = MyNListviewObject(MakeID('L','M','L','V'), "BAR,BAR,BAR P=" MUIX_C ",BAR P=" MUIX_R ",BAR,BAR", &lowmemlist_con2hook, &lowmemlist_des2hook, &lowmemlist_dsp2hook, &lowmemlist_cmp2hook, TRUE),
  223.             Child, MyBelowListview(&lowmemtext, &lowmemcount),
  224.  
  225.             Child, MyVSpace(4),
  226.  
  227.             Child, HGroup, MUIA_Group_SameSize, TRUE,
  228.                 Child, updateButton   = MakeButton(txtUpdate),
  229.                 Child, printButton    = MakeButton(txtPrint),
  230.                 Child, removeButton   = MakeButton(txtRemove),
  231.                 Child, priorityButton = MakeButton(txtPriority),
  232.                 Child, exitButton     = MakeButton(txtExit),
  233.             End,
  234.         End,
  235.         TAG_MORE, msg->ops_AttrList))
  236.     {
  237.         struct LowMemoryWinData *lmwd = INST_DATA(cl, obj);
  238.         APTR parent;
  239.  
  240.         lmwd->lmwd_LowMemoryList = lowmemlist;
  241.         lmwd->lmwd_LowMemoryText = lowmemtext;
  242.         lmwd->lmwd_LowMemoryCount = lowmemcount;
  243.         lmwd->lmwd_RemoveButton = removeButton;
  244.         lmwd->lmwd_PriorityButton = priorityButton;
  245.  
  246.         parent = (APTR)GetTagData(MUIA_Window_ParentWindow, (ULONG)NULL, msg->ops_AttrList);
  247.  
  248.         set(obj, MUIA_Window_Title, MyGetWindowTitle("LOWMEMORY", lmwd->lmwd_Title, sizeof(lmwd->lmwd_Title)));
  249.         set(obj, MUIA_Window_ActiveObject, lowmemlist);
  250.         set(removeButton, MUIA_Disabled, TRUE);
  251.         set(priorityButton, MUIA_Disabled, TRUE);
  252.  
  253.         DoMethod(parent,         MUIM_Window_AddChildWindow, obj);
  254.         DoMethod(obj,            MUIM_Notify, MUIA_Window_CloseRequest, TRUE,           MUIV_Notify_Application, 5, MUIM_Application_PushMethod, parent, 2, MUIM_Window_RemChildWindow, obj);
  255.         DoMethod(lowmemlist,     MUIM_Notify, MUIA_NList_Active,        MUIV_EveryTime, obj,                     1, MUIM_LowMemoryWin_ListChange);
  256.         DoMethod(updateButton,   MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     1, MUIM_LowMemoryWin_Update);
  257.         DoMethod(printButton,    MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     1, MUIM_LowMemoryWin_Print);
  258.         DoMethod(removeButton,   MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     1, MUIM_LowMemoryWin_Remove);
  259.         DoMethod(priorityButton, MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     1, MUIM_LowMemoryWin_Priority);
  260.         DoMethod(exitButton,     MUIM_Notify, MUIA_Pressed,             FALSE,          obj,                     3, MUIM_Set, MUIA_Window_CloseRequest, TRUE);
  261.     }
  262.  
  263.     return (ULONG)obj;
  264. }
  265.  
  266. static ULONG __saveds mDispose( struct IClass *cl,
  267.                                 Object *obj,
  268.                                 struct opSet *msg )
  269. {
  270.     struct LowMemoryWinData *lmwd = INST_DATA(cl, obj);
  271.  
  272.     set(obj, MUIA_Window_Open, FALSE);
  273.     DoMethod(lmwd->lmwd_LowMemoryList, MUIM_NList_Clear);
  274.  
  275.     return (DoSuperMethodA(cl, obj, msg));
  276. }
  277.  
  278. static ULONG __saveds mUpdate( struct IClass *cl,
  279.                                Object *obj,
  280.                                Msg msg )
  281. {
  282.     struct LowMemoryWinData *lmwd = INST_DATA(cl, obj);
  283.     struct LowMemoryCallbackUserData ud;
  284.  
  285.     ApplicationSleep(TRUE);
  286.     set(lmwd->lmwd_LowMemoryList, MUIA_NList_Quiet, TRUE);
  287.     DoMethod(lmwd->lmwd_LowMemoryList, MUIM_NList_Clear);
  288.  
  289.     ud.ud_List = lmwd->lmwd_LowMemoryList;
  290.     ud.ud_Count = 0;
  291.  
  292.     if (clientstate) {
  293.         ReceiveList(UpdateCallback, &ud);
  294.     } else {
  295.         IterateList(UpdateCallback, &ud);
  296.     }
  297.  
  298.     SetCountText(lmwd->lmwd_LowMemoryCount, ud.ud_Count);
  299.     MySetContents(lmwd->lmwd_LowMemoryText, "");
  300.  
  301.     set(lmwd->lmwd_LowMemoryList, MUIA_NList_Quiet, FALSE);
  302.     set(lmwd->lmwd_RemoveButton, MUIA_Disabled, TRUE);
  303.     set(lmwd->lmwd_PriorityButton, MUIA_Disabled, TRUE);
  304.     ApplicationSleep(FALSE);
  305.  
  306.     return 0;
  307. }
  308.  
  309. static ULONG __saveds mPrint( struct IClass *cl,
  310.                               Object *obj,
  311.                               Msg msg )
  312. {
  313.     PrintLowMemory(NULL);
  314.  
  315.     return 0;
  316. }
  317.  
  318. static ULONG __saveds mRemove( struct IClass *cl,
  319.                                Object *obj,
  320.                                Msg msg )
  321. {
  322.     struct LowMemoryWinData *lmwd = INST_DATA(cl, obj);
  323.     struct LowMemoryEntry *lme;
  324.  
  325.     if (lme = (struct LowMemoryEntry *)GetActiveEntry(lmwd->lmwd_LowMemoryList)) {
  326.         if (MyRequest(msgYesNo, msgWantToRemoveLowMemory, lme->lme_Name)) {
  327.             MyDoCommand("RemoveLowMemory %s", lme->lme_Address);
  328.             DoMethod(obj, MUIM_LowMemoryWin_Update);
  329.         }
  330.     }
  331.  
  332.     return 0;
  333. }
  334.  
  335. static ULONG __saveds mPriority( struct IClass *cl,
  336.                                  Object *obj,
  337.                                  Msg msg )
  338. {
  339.     struct LowMemoryWinData *lmwd = INST_DATA(cl, obj);
  340.     struct LowMemoryEntry *lme;
  341.  
  342.     if (lme = (struct LowMemoryEntry *)GetActiveEntry(lmwd->lmwd_LowMemoryList)) {
  343.         LONG pri;
  344.  
  345.         pri = atol(lme->lme_Pri);
  346.         if (GetPriority(lme->lme_Name, &pri)) {
  347.             if (MyDoCommand("SetPriority LOWMEMORY \"%s\" %ld", lme->lme_Name, pri)) {
  348.                 _snprintf(lme->lme_Pri, sizeof(lme->lme_Pri), "%4ld", pri);
  349.                 RedrawActiveEntry(lmwd->lmwd_LowMemoryList);
  350.             }
  351.         }
  352.     }
  353.  
  354.     return 0;
  355. }
  356.  
  357. static ULONG __saveds mListChange( struct IClass *cl,
  358.                                    Object *obj,
  359.                                    Msg msg )
  360. {
  361.     struct LowMemoryWinData *lmwd = INST_DATA(cl, obj);
  362.     struct LowMemoryEntry *lme;
  363.  
  364.     if (lme = (struct LowMemoryEntry *)GetActiveEntry(lmwd->lmwd_LowMemoryList)) {
  365.         MySetContents(lmwd->lmwd_LowMemoryText, "%s \"%s\"", lme->lme_Address, lme->lme_Name);
  366.         set(lmwd->lmwd_RemoveButton, MUIA_Disabled, FALSE);
  367.         set(lmwd->lmwd_PriorityButton, MUIA_Disabled, FALSE);
  368.     }
  369.  
  370.     return 0;
  371. }
  372.  
  373. ULONG __asm __saveds LowMemoryWinDispatcher( register __a0 struct IClass *cl,
  374.                                              register __a2 Object *obj,
  375.                                              register __a1 Msg msg )
  376. {
  377.     switch (msg->MethodID) {
  378.         case OM_NEW:                       return (mNew(cl, obj, (APTR)msg));
  379.         case OM_DISPOSE:                   return (mDispose(cl, obj, (APTR)msg));
  380.         case MUIM_LowMemoryWin_Update:     return (mUpdate(cl, obj, (APTR)msg));
  381.         case MUIM_LowMemoryWin_Print:      return (mPrint(cl, obj, (APTR)msg));
  382.         case MUIM_LowMemoryWin_Remove:     return (mRemove(cl, obj, (APTR)msg));
  383.         case MUIM_LowMemoryWin_Priority:   return (mPriority(cl, obj, (APTR)msg));
  384.         case MUIM_LowMemoryWin_ListChange: return (mListChange(cl, obj, (APTR)msg));
  385.     }
  386.  
  387.     return (DoSuperMethodA(cl, obj, msg));
  388. }
  389.  
  390. void PrintLowMemory( char *filename )
  391. {
  392.     BPTR handle;
  393.  
  394.     if (handle = HandlePrintStart(filename)) {
  395.         PrintFOneLine(handle, "\n  Address  Type       Pri    Data      Code    Name\n\n");
  396.         IterateList(PrintCallback, (void *)handle);
  397.     }
  398.  
  399.     HandlePrintStop();
  400. }
  401.  
  402. void SendLowMemory( void )
  403. {
  404.     IterateList(SendCallback, NULL);
  405. }
  406.  
  407.